﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.IO;
using System.Text;

/// <summary>
/// Summary description for clsEncryption
/// </summary>
public static class clsEncryptionDecryption
{
    public static string KEY = "K6u8#m2a";
    public static string EncryptQueryString(string stringToEncrypt)
    {
        byte[] key = { };
        byte[] IV = { 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 };
        try
        {
            key = Encoding.UTF8.GetBytes(KEY);
            using (DESCryptoServiceProvider MyDESCrypto = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
                MemoryStream MyMemoryStream = new MemoryStream();
                CryptoStream MyCryptoStream = new CryptoStream(MyMemoryStream,
                MyDESCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write);
                MyCryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
                MyCryptoStream.FlushFinalBlock();
                return Convert.ToBase64String(MyMemoryStream.ToArray());
            }
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

    public static string DecryptQueryString(string stringToDecrypt)
    {
        byte[] key = { };
        byte[] IV = { 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 };
        stringToDecrypt = stringToDecrypt.Replace(" ", "+");
        byte[] inputByteArray = new byte[stringToDecrypt.Length];


        try
        {
            key = Encoding.UTF8.GetBytes(KEY);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(stringToDecrypt);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            Encoding encoding = Encoding.UTF8;
            return encoding.GetString(ms.ToArray());
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }


    public static string Base64EncodingMethod(string sData)
    {
        byte[] encodingDataASBytes = System.Text.Encoding.Unicode.GetBytes(sData);
        string sReturnValues = System.Convert.ToBase64String(encodingDataASBytes);
        return sReturnValues;
    }

    public static string Base64DecodingMethod(string sData)
    {
        byte[] encData = System.Convert.FromBase64String(sData);
        string result = System.Text.ASCIIEncoding.ASCII.GetString(encData);
        return result;
    }


    static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ASIIndor");
    public static string Encrypt(string originalString)
    {
        if (String.IsNullOrEmpty(originalString))
        {
            throw new ArgumentNullException
                   ("The string which needs to be encrypted can not be null.");
        }
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream,
            cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
        StreamWriter writer = new StreamWriter(cryptoStream);
        writer.Write(originalString);
        writer.Flush();
        cryptoStream.FlushFinalBlock();
        writer.Flush();
        return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
    }

    public static string Decrypt(string cryptedString)
    {
        if (String.IsNullOrEmpty(cryptedString))
        {
            throw new ArgumentNullException
               ("The string which needs to be decrypted can not be null.");
        }
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        MemoryStream memoryStream = new MemoryStream
                (Convert.FromBase64String(cryptedString));
        CryptoStream cryptoStream = new CryptoStream(memoryStream,
            cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
        StreamReader reader = new StreamReader(cryptoStream);
        return reader.ReadToEnd();
    }
}
